package com.app.bussinesslocator.httpclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.app.bussinesslocator.Utils.PreferenceHelper;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpClientHelper {
		// khai bao duong dan api chung
		private static final String BASE_URL = "http://www.techbusters.net.au";
		
		
		// search
		private static final String SEARCH = "/api";
		// khai bao context
		private Context context;
		
		// khai bao cac thanh phan thao tac client va server
		private static AsyncHttpClient client = new AsyncHttpClient();
		private AsyncHttpResponseHandler  asyncHttpResponseHandler;
		
		// khai bao file luu tam
		private PreferenceHelper preferenceHelper;
		
		// contructor
		public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler, Context context) {
			this.asyncHttpResponseHandler = asyncHttpResponseHandler;
			this.context = context;
			preferenceHelper = new PreferenceHelper(context);
		}
		
		public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler) {
			this.asyncHttpResponseHandler = asyncHttpResponseHandler;
		}
		
		public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
			client.get(getAbsoluteUrl(BASE_URL, url), params, responseHandler);
		}
		
		// phan rieng
		public static void get(String url, AsyncHttpResponseHandler responseHandler) {
			client.get(url, null, responseHandler);
		}

		public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
			client.post(getAbsoluteUrl(BASE_URL,url), params, responseHandler);
		}
		
		
		// get url voi tham so truyen len
		private static String getUrlWithParams(String base, String relativeUrl, List<BasicNameValuePair> params) {
			String url = getAbsoluteUrl(base, relativeUrl);
			for (int i = 0; i < params.size(); i++) {
				BasicNameValuePair b = params.get(i);
				url+="&"+b.getName()+"="+b.getValue();
			}
			url = url.replace(" ", "%20");
			return url;
		}
		
		// get url post
		private static String getUrlPostWithParams(String base, String relativeUrl, RequestParams params) {
			String url = getAbsoluteUrl(base, relativeUrl);
			RequestParams r = params;
			url+="?"+r;
			url = url.replace(" ", "%20");
			return url;
		}
		
		
		// tao duong dan api 
		private static String getAbsoluteUrl(String base, String relativeUrl) {
			return base + relativeUrl + "/";
		}
		
		// Search by key word
		public void searchByKeyWord(String keyword, int pageNumber, float lon, float lat){
			RequestParams params = new RequestParams();
			params.add("keyword", keyword);
			params.add("page_number", String.valueOf(pageNumber));
			params.add("latitude", String.valueOf(lat));
			params.add("longitude", String.valueOf(lon));
			post(SEARCH, params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SEARCH, params));
		}
		
		
		public void searchNear(String type, int pageNumber, String lon, String lat){
			RequestParams params = new RequestParams();
			params.add("type", type);
			params.add("page_number", String.valueOf(pageNumber));
			params.add("longitude", lon);
			params.add("latitude", lat);
			post(SEARCH, params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SEARCH, params));
		}
		
		
		
}
